-- 扶植圣女
local fuzhishengnv = fk.CreateSkill {
    name = "mcdh_fuzhishengnv",
    frequency = Skill.Compulsory,
    on_acquire =  function(self,player, is_start)
        local room = player.room
        room:setPlayerMark(player,"@mcdh_fuzhishengnv",{"#mcdh_fuzhishengnv_1","#mcdh_fuzhishengnv_2","#mcdh_fuzhishengnv_3"})
    end,
    on_lose = function(self,player)
        local room = player.room
        room:setPlayerMark(player,0)
    end
}

fuzhishengnv:addEffect(fk.EventPhaseStart,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(self) and target == player and target.phase == Player.Finish and #player:getTableMark("@mcdh_fuzhishengnv") > 0 then
            local room = player.room
            local all_choices = player:getTableMark("@mcdh_fuzhishengnv")
            local choices = {}
            if table.contains(all_choices,"#mcdh_fuzhishengnv_1") then
                if not player:isNude() then
                    table.insert(choices,"#mcdh_fuzhishengnv_1")
                end
            end
            if table.contains(all_choices,"#mcdh_fuzhishengnv_2") then
                if player.maxHp > 0 then
                    table.insert(choices,"#mcdh_fuzhishengnv_2")
                end
            end
            if table.contains(all_choices,"#mcdh_fuzhishengnv_3") then
                if player:hasSkill("mcdh_youjiebizhu") and #table.filter(room:getOtherPlayers(player),function (p)
                    return not p:hasSkill("mcdh_youjiebizhu")
                end) > 0 then
                    table.insert(choices,"#mcdh_fuzhishengnv_3")
                end
            end
            return #choices > 0
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local all_choices = player:getTableMark("@mcdh_fuzhishengnv")
        local choices = {}
        if table.contains(all_choices,"#mcdh_fuzhishengnv_1") then
            if not player:isNude() then
                table.insert(choices,"#mcdh_fuzhishengnv_1")
            end
        end
        if table.contains(all_choices,"#mcdh_fuzhishengnv_2") then
            if player.maxHp > 0 then
                table.insert(choices,"#mcdh_fuzhishengnv_2")
            end
        end
        if table.contains(all_choices,"#mcdh_fuzhishengnv_3") then
            if player:hasSkill("mcdh_youjiebizhu") and #table.filter(room:getOtherPlayers(player),function (p)
                return not p:hasSkill("mcdh_youjiebizhu")
            end) > 0 then
                table.insert(choices,"#mcdh_fuzhishengnv_3")
            end
        end
        if #choices > 0 then
            local choose = room:askForChoice(player,choices,self.name,"",true,all_choices)
            if choose == "#mcdh_fuzhishengnv_1" then
                local tars,card = room:askForChooseCardAndPlayers(player,table.map(room:getOtherPlayers(player), Util.IdMapper),1,1,".|.|.|hand,equip","#mcdh_fuzhishengnv_1_askfor",self.name,false)
                room:moveCardTo(card, Card.PlayerHand, tars[1], fk.ReasonGive, self.name, nil, false, player.id)
            elseif choose == "#mcdh_fuzhishengnv_2" then
                local tar = room:askForChoosePlayers(player,table.map(room:getOtherPlayers(player), Util.IdMapper),1,1,"#mcdh_fuzhishengnv_2_askfor",self.name,false)
                room:changeMaxHp(player, -1)
                room:changeMaxHp(room:getPlayerById(tar[1]), 1)
            elseif choose == "#mcdh_fuzhishengnv_3" then
                local tar = room:askForChoosePlayers(player,table.map(table.filter(room:getOtherPlayers(player),function (p)
                    return not p:hasSkill("mcdh_youjiebizhu")
                end), Util.IdMapper),1,1,"#mcdh_fuzhishengnv_3_askfor",self.name,false)
                room:handleAddLoseSkills(player, "-mcdh_youjiebizhu", nil)
                room:handleAddLoseSkills(room:getPlayerById(tar[1]), "mcdh_youjiebizhu", nil)
            end
            table.removeOne(all_choices,choose)
            room:setPlayerMark(player,"@mcdh_fuzhishengnv",all_choices)
            if #all_choices == 2 then
                room:handleAddLoseSkills(player, "mcdh_ref_ganglie", nil)
            elseif #all_choices == 1 then
                room:handleAddLoseSkills(player, "mcdh_ref_wenji", nil)
            elseif #all_choices == 0 then
                room:handleAddLoseSkills(player, "mcdh_ref_mubing", nil)
            end
        end
    end,
})

return fuzhishengnv